from typing import Optional
from fastapi import APIRouter, Depends, Query
from sqlalchemy.orm import Session
from app.db.database import get_db
from app.services.sponsors_research_service import SponsorsResearchService
from app.repositories.sponsors_research_repo import SponsorsResearchRepo
from app.schema.sponsors_research_schema import (
    SponsorByGroupItem,
    CompanyContactsItem,
    CompanyScholarshipItem,
    ProgramScholarshipItem
)
from app.schema.pagination import Page, PageMeta

router = APIRouter(prefix="/sponsors/research", tags=["sponsors_research"])

@router.get("/by-group", response_model=Page[SponsorByGroupItem], summary="Sponsors count by program/school/coop")
def get_sponsors_by_group(
    db: Session = Depends(get_db),
    year_from:    int            = Query(..., description="Program Year Start"),
    year_to:      int            = Query(..., description="Program Year End"),
    current:      Optional[bool] = Query(None, description="Only Current/Not Current"),
    program_code: Optional[str]  = Query(None, description="Fuzzy match Program Code"),
    faculty:      Optional[str]  = Query(None, description="Filter by Faculty"),
    coop_program: Optional[str]  = Query(None, description="Filter by Co-op Program"),
    page:         int            = Query(1, ge=1),
    page_size:    int            = Query(10, ge=1, le=100),
):
    svc = SponsorsResearchService(SponsorsResearchRepo(db))
    total, items = svc.sponsors_by_group(
        year_from, year_to, current,
        program_code, faculty, coop_program,
        page, page_size
    )
    return Page[SponsorByGroupItem](
        meta=PageMeta(total=total, page=page, page_size=page_size),
        items=items
    )

@router.get("/contacts-by-group", response_model=Page[CompanyContactsItem])
def list_sponsor_contacts(
    db: Session = Depends(get_db),
    year_from:    int            = Query(..., description="Program Year Start"),
    year_to:      int            = Query(..., description="Program Year End"),
    current:      Optional[bool] = Query(None, description="Only Current/Not Current"),
    program_code: Optional[str]  = Query(None, description="Fuzzy match Program Code"),
    faculty:      Optional[str]  = Query(None, description="Filter by Faculty"),
    coop_program: Optional[str]  = Query(None, description="Filter by Co-op Program"),
    page:         int            = Query(1, ge=1),
    page_size:    int            = Query(10, ge=1, le=100),
):
    svc = SponsorsResearchService(SponsorsResearchRepo(db))
    total, items = svc.list_contacts(
        year_from, year_to, current,
        program_code, faculty, coop_program,
        page, page_size
    )
    return Page[CompanyContactsItem](
        meta=PageMeta(total=total, page=page, page_size=page_size),
        items=items
    )

@router.get("/scholarships-by-company", response_model=Page[CompanyScholarshipItem], summary="Scholarships sum by company")
def get_scholarships_by_company(
    db: Session = Depends(get_db),
    year_from:      int             = Query(..., description="Program Year Start"),
    year_to:        int             = Query(..., description="Program Year End"),
    current:        Optional[bool]  = Query(None, description="Only Current/Not Current"),
    program_code:   Optional[str]   = Query(None, description="Fuzzy match Program Code"),
    page:           int             = Query(1, ge=1),
    page_size:      int             = Query(10, ge=1, le=100),
):
    svc = SponsorsResearchService(SponsorsResearchRepo(db))
    total, items = svc.get_scholarships_by_company(
        year_from, year_to, current,
        program_code, page, page_size
    )
    return Page[CompanyScholarshipItem](
        meta=PageMeta(total=total, page=page, page_size=page_size),
        items=items
    )


@router.get("/scholarships-by-program", response_model=Page[ProgramScholarshipItem], summary="Positions sum by program")
def get_scholarships_by_program(
    db: Session = Depends(get_db),
    year_from:    int            = Query(..., description="Program Year Start"),
    year_to:      int            = Query(..., description="Program Year End"),
    current:      Optional[bool] = Query(None, description="Only Current/Not Current"),
    program_code: Optional[str]  = Query(None, description="Fuzzy match Program Code"),
    page:         int            = Query(1, ge=1),
    page_size:    int            = Query(10, ge=1, le=100),
):
    svc = SponsorsResearchService(SponsorsResearchRepo(db))
    total, items = svc.get_scholarships_by_program(
        year_from, year_to, current,
        program_code, page, page_size
    )
    return Page[ProgramScholarshipItem](
        meta=PageMeta(total=total, page=page, page_size=page_size),
        items=items
    )